package models

type Index struct {
	Id       int64    `json:"id"`
	Index    []int    `json:"index"`
	Label    string   `json:"label"`
	Children []*Index `json:"children"`
}

// set by default
func (this *Index) SetId(id int64) *Index {
	this.Id = id
	return this
}

func (this *Index) AddIndex(id int) *Index {
	if this.Index == nil {
		tmp := []int{id}
		this.Index = tmp
	} else {
		this.Index = append(this.Index, id)
	}
	return this
}

func (this *Index) SetLabel(label string) *Index {
	this.Label = label
	return this
}

func (this *Index) AddChildren(data *Index) *Index {
	if this.Children == nil {
		tmp := []*Index{data}
		this.Children = tmp
	} else {
		this.Children = append(this.Children, data)
	}
	return this
}

// func NewIndex() []*Index {
// 	return []*Index{
// 		&Index{
// 			Id:    0,
// 			Label: "1",
// 			Children: []*Index{
// 				&Index{
// 					Id:    1,
// 					Label: "1-1",
// 				},
// 			},
// 		},
// 	}
// }

func NewIndex() []*Index {
	return []*Index{
		&Index{
			Id: 0,
			// Index: []int{0},
			Label: "1",
			Children: []*Index{
				&Index{
					Id: 1,
					// Index: []int{0, 0},
					Label: "1-1",
					Children: []*Index{
						&Index{
							Id: 2,
							// Index: []int{0, 0},
							Label: "1-1-1",
						},
					},
				},
				&Index{
					Id: 3,
					// Index: []int{0, 0},
					Label: "1-2",
					Children: []*Index{
						&Index{
							Id: 4,
							// Index: []int{0, 0},
							Label: "1-2-1",
						},
					},
				},
			},
		},
		&Index{
			Id: 9,
			// Index: []int{0},
			Label: "2",
			Children: []*Index{
				&Index{
					Id: 5,
					// Index: []int{0, 0},
					Label: "2-1",
					Children: []*Index{
						&Index{
							Id: 6,
							// Index: []int{0, 0},
							Label: "2-1-1",
						},
					},
				},
				&Index{
					Id: 7,
					// Index: []int{0, 0},
					Label: "2-2",
					Children: []*Index{
						&Index{
							Id: 8,
							// Index: []int{0, 0},
							Label: "2-2-1",
						},
					},
				},
			},
		},
	}
}

// // add node
// func AddIndex() {

// }

// // change data by index([]int)
// func SetIndex() {

// }

// refresh []*Index -> Index column
func Refresh(data []*Index, parentArray []int) {
	if parentArray == nil {
		for num, info := range data {
			info.Index = []int{num}
			// fmt.Println([]int{num}, info.Label, parentArray, "what")
			if info.Children != nil {
				Refresh(info.Children, []int{num})
			}
		}
	} else {
		for num, info := range data {
			var tmp []int
			tmp = append(tmp, parentArray...)
			tmp = append(tmp, num)
			info.Index = tmp
			// fmt.Println(tmp, info.Label, parentArray, info.Index)
			if info.Children != nil {
				Refresh(info.Children, tmp)
			}
		}
	}
}

// set index basic func
func GetById(input []*Index, id int) *Index {
	return input[id]
}

// according to int array to find which one of the Index object
func GetByArray(input []*Index, ids []int) *Index {
	if len(ids) == 0 {
		return nil
	} else if len(ids) > 1 {
		for _, x := range ids {
			// if object no include Children return err
			if input[x].Children == nil {
				return nil
			}
			return GetByArray(input[x].Children, ids[1:])
		}
	} else if len(ids) == 1 {
		return input[ids[0]]
	}
	return nil
}
